﻿using System;
using System.Threading;
using FrontDisplay.NativeMethods;
#if !DEBUG
using MediaPortal.GUI.Library;
using MediaPortal.Player;
#endif

namespace FrontDisplay.MediaInformer
{
#if DEBUG
    public static class Program
    {
        public static void Main()
        {
            InformerPlugin plugin = new InformerPlugin();
            string command;
            do
            {
                command = Console.ReadLine();
                switch (command)
                {
                    case "play":
                        plugin.InformFrontView("C:\\Friends 9x02 Emma Cries.avi", 1448);
                        break;
                    case "stop":
                        plugin.InformFrontView("C:\\Friends 9x02 Emma Cries.avi", 0);
                        break;
                    case "info":
                        plugin.UpdateTime(DateTime.Now.Second * 24, 1448);
                        break;
                    case "exit":
                        break;
                    default:
                        Console.WriteLine("Wrong command");
                        break;
                }

            } while (command != "exit");
            plugin.Stop();
        }
    }
#endif

    public class InformerPlugin
#if !DEBUG
        : IPlugin, ISetupForm
#endif
    {

        internal void UpdateTime(int position, int duration)
        {
            IntPtr hWnd = WinApi.FindWindow(null, FrontDisplayWindowName);
            if (hWnd != IntPtr.Zero)
            {
                WinApi.SendMessage(hWnd, WinApi.WM_USER, (IntPtr)position, (IntPtr)duration);
            }
        }

        internal void InformFrontView(string fileName, int duration)
        {
            IntPtr hWnd = WinApi.FindWindow(null, FrontDisplayWindowName);
            if (hWnd != IntPtr.Zero)
            {
                WinApi.SendTextToWindow(hWnd, fileName);
                WinApi.SendMessage(hWnd, WinApi.WM_USER, IntPtr.Zero, (IntPtr)duration);
            }
        }

        private Timer _timer;
        const string FrontDisplayWindowName = "Front Display";

        public void Start()
        {
#if !DEBUG
            g_Player.PlayBackStarted += PlayBackStarted;
            g_Player.PlayBackEnded += PlayBackEnded;
            g_Player.PlayBackStopped += PlayBackStopped;
            g_Player.PlayBackChanged += PlayBackChanged;
#endif
            _timer = new Timer(TimerTick, null, Timeout.Infinite, Timeout.Infinite);
        }

        public void Stop()
        {
#if !DEBUG
            g_Player.PlayBackStarted -= PlayBackStarted;
            g_Player.PlayBackEnded -= PlayBackEnded;
            g_Player.PlayBackStopped -= PlayBackStopped;
            g_Player.PlayBackChanged -= PlayBackChanged;
#endif
            if (_timer != null)
            {
                _timer.Change(Timeout.Infinite, Timeout.Infinite);
                _timer.Dispose();
                _timer = null;
            }
            UpdateTime(0, 0);
        }

        private void TimerTick(object state)
        {
#if !DEBUG
            UpdateTime((int)g_Player.CurrentPosition, (int)g_Player.Duration);
#endif
        }

#if !DEBUG
        private void PlayBackChanged(g_Player.MediaType type, int stoptime, string filename)
        {
            InformFrontView(filename);
        }

        private void PlayBackStopped(g_Player.MediaType type, int stoptime, string filename)
        {
            _timer.Change(Timeout.Infinite, Timeout.Infinite);
            InformFrontView(filename, 0);
        }

        private void PlayBackEnded(g_Player.MediaType type, string filename)
        {
            _timer.Change(Timeout.Infinite, Timeout.Infinite);
            InformFrontView(filename, 0);
        }

        private void PlayBackStarted(g_Player.MediaType type, string filename)
        {
            _timer.Change(0, 1000);
            InformFrontView(filename);
        }

        private void InformFrontView(string fileName)
        {
            InformFrontView(fileName, (int)g_Player.Duration);
        }
#endif

        #region ISetupForm Members

        public string PluginName() { return "Front Display media informer."; }
        public string Description() { return "Informs Front Display application about playing status."; }
        public string Author() { return "Yuri Kolesnikov"; }
        public void ShowPlugin() { }
        public bool CanEnable() { return true; }
        public int GetWindowId() { return 0; }
        public bool DefaultEnabled() { return true; }
        public bool HasSetup() { return false; }

        public bool GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
        {
            strButtonText = strButtonImage = strButtonImageFocus = strPictureImage = string.Empty;
            return false;
        }

        #endregion
    }
}
